package org.lq.er.view.login;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Admin;
import org.lq.er.entity.Student;
import org.lq.er.service.AdminService;
import org.lq.er.service.StudentService;
import org.lq.er.service.impl.AdminServiceImpl;
import org.lq.er.service.impl.StudentServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
/**
 * 重置密码(管理员)视图
 * @author 宋铀
 * 
 * @package org.lq.er.view.login
 * @date 2020年9月10日下午8:21:58
 */
public class V_newPwd2 extends JFrame {

	private JPanel contentPane;
	private static Admin admin = null; 
	private JPasswordField password;
	private JPasswordField passwordCkeck;
	private AdminService as = new AdminServiceImpl(); 

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_newPwd2 frame = new V_newPwd2(admin);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_newPwd2(Admin admin) {
		setTitle("重置密码(管里员)");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 396, 300);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel title = new JLabel("重置密码");
		title.setFont(new Font("幼圆", Font.PLAIN, 28));
		title.setBounds(137, 24, 121, 59);
		contentPane.add(title);
		
		JLabel lblnewPwd = new JLabel("新密码:");
		lblnewPwd.setFont(new Font("宋体", Font.PLAIN, 16));
		lblnewPwd.setBounds(79, 100, 66, 25);
		contentPane.add(lblnewPwd);
		
		JLabel lblnewPwd2 = new JLabel("确认密码:");
		lblnewPwd2.setFont(new Font("宋体", Font.PLAIN, 16));
		lblnewPwd2.setBounds(65, 150, 80, 25);
		contentPane.add(lblnewPwd2);
		
		JButton btnNewButton = new JButton("确认");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String passwd = password.getText().toString().trim();
				String passwdChecked = passwordCkeck.getText().toString().trim();
				if(!"".equals(passwd) && !"".equals(passwd)) {
					if(passwd.equals(passwdChecked)) {
						updatePwd(admin,passwdChecked);
						JOptionPane.showMessageDialog(V_newPwd2.this, "请登录!","提示",JOptionPane.INFORMATION_MESSAGE);
						dispose();
					} else {
						JOptionPane.showMessageDialog(V_newPwd2.this, "两次输入的密码不一致!","提示",JOptionPane.ERROR_MESSAGE);
					}
					
				} else {
					JOptionPane.showMessageDialog(V_newPwd2.this, "请输入密码!","提示",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		btnNewButton.setBounds(136, 214, 93, 23);
		contentPane.add(btnNewButton);
		
		password = new JPasswordField();
		password.setBounds(157, 102, 135, 23);
		contentPane.add(password);
		
		passwordCkeck = new JPasswordField();
		passwordCkeck.setBounds(157, 153, 135, 22);
		contentPane.add(passwordCkeck);
	}
	
	// 修改密码
	public void updatePwd(Admin admin,String passwd) {
		admin.setPassword(passwd);
		if (as.update(admin)) {
			System.out.println(admin.toString());
		} else {

		}
		
	}
}
